﻿package  Welsy4.Connect.commands
{
	import com.adobe.cairngorm.commands.SequenceCommand;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import mx.rpc.IResponder;
	import mx.controls.Alert;
	
	import flash.net.URLVariables;
	
	import Welsy4.Connect.business.CommonInfoDelegate;
	import Welsy4.Connect.events.*;
	import Welsy4.Connect.model.ConnectModel;
	import Welsy4.Connect.vo.HttpServiceHandler;
	import Welsy4.Connect.vo.LoginVO;
	
	
	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class CommonInfoCommand extends SequenceCommand implements ICommand, IResponder
	{
		
		public function CommonInfoCommand() 
		{
			
		}
		override public function execute(event:CairngormEvent):void
		{
			// debugging info
			ConnectModel.getInstance().addQueue("CommonInfoCommand.execute");
			
			this.nextEvent = ChainEvent(event).nextChainedEvent;
			var delegate:CommonInfoDelegate = new CommonInfoDelegate( this );
			delegate.getCommonInfo();
			
			
		}
	
		public function result(data:Object):void
			{
				ConnectModel.getInstance().addQueue("CommonInfoCommand.result");
				
				ConnectModel.getInstance().httpServiceHandler = new HttpServiceHandler();
			    ConnectModel.getInstance().httpServiceHandler.setXML(data.result);
				
				
				/* output */
				//Alert.show("Status" + ConnectModel.getInstance().httpServiceHandler.getResultStatus());
				if (ConnectModel.getInstance().httpServiceHandler.getResultStatus() == ConnectModel.SESSION_OK) {
					/*	continua con il ciclo */
					CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.COMMON_INFO_EXECUTED,true,ResponseEvent.OK,ConnectModel.getInstance().httpServiceHandler.xml.toString()));
				    
					/* controlla l'eisitenza di un user */
					if(ConnectModel.getInstance().httpServiceHandler.xml.common.user.length() > 0){
						this.executeNextCommand();
					} else {
						// procedi con l'auth
						
						if (ConnectModel.getInstance().currentUser != null) {
							//Alert.show("Necessario Login, ma i dati ci stanno");
							if(this.nextEvent!=null){
							    //var currentNextChainedEvent:ChainEvent = EventChainFactory.reconstructChainEvent( ChainEvent(this.nextEvent) );
							
								var chainEvent:ChainEvent;
								
								if(ConnectModel.getInstance().currentUser.isAdministrator){
									chainEvent = EventChainFactory.chainEvents([new AuthenticationEvent(AuthenticationEvent.SECURE_LOG_IN, new URLVariables("login="+ ConnectModel.getInstance().currentUser.nickname +"&password="+ ConnectModel.getInstance().currentUser.getPassword())), this.nextEvent]);
								} else {
									chainEvent = EventChainFactory.chainEvents([new AuthenticationEvent(AuthenticationEvent.LOG_IN,new URLVariables("login="+ ConnectModel.getInstance().currentUser.nickname )), this.nextEvent]);	
								}
								CairngormEventDispatcher.getInstance().dispatchEvent(chainEvent);
							}
							
						} else {
							//Alert.show("Necessario Login, ma i dati non ci stanno");
							CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, true, ResponseEvent.LOGIN_REQUIRED, ResponseEvent.ERROR_DESCRIPTION_AUTHENTICATION_REQUIRED));
						}
					}
					
				} else {
					CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.COMMON_INFO_COMMAND_FAULT, data.result));
				}
				
				
				
			}
		
		public function fault(info:Object):void
			{
				// debugging info
			    ConnectModel.getInstance().addQueue("CommonInfoCommand.fault");
				CairngormEventDispatcher.getInstance().dispatchEvent(new ResponseEvent(ResponseEvent.ERROR, false, ResponseEvent.COMMON_INFO_COMMAND_FAULT, ResponseEvent.ERROR_DESCRIPTION_GENERIC_COMMAND_FAULT));
				
			}
			
		}
	
}